package com.xxx.utils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created by louisyuu on 2020/4/29 上午10:17
 */
public class IpUtils {


    public static String getLocalHostAddress() {
        InetAddress ip4 = null;
        try {
            ip4 = Inet4Address.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        if (ip4 != null) {
            return ip4.getHostAddress();
        } else {
            return "127.0.0.1";
        }
    }


    public static String getAppConnectedAddress(int port) {
        return getLocalHostAddress() + ":" + String.valueOf(port);
    }


    public static void main(String[] args) throws UnknownHostException {
        InetAddress ip4 = Inet4Address.getLocalHost();
        System.out.println(ip4.getHostAddress());
    }

}
